package com.wap.framework.admin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wap.framework.model.PageAd;
import com.wap.framework.service.PageAdService;

public class PageAdManagerAction extends DispatchAction {
	private PageAdService pageAdService;

	public void setPageAdService(PageAdService pageAdService) {
		this.pageAdService = pageAdService;
	}

	/**
	 * list all advertisement
	 */
	@SuppressWarnings("unchecked")
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String currentPage = request.getParameter("currentPage");
		if (currentPage == null || "".equals(currentPage)) {
			currentPage = "1";
		}

		List<Object> result = pageAdService.listPageAd(Integer
				.valueOf(currentPage));
		request.setAttribute("pageAds", (List<PageAd>) result.get(0));
		request.setAttribute("pageHelper", result.get(1));

		return mapping.findForward("fwd_mgr_pageAd");
	}

	/**
	 * Add advertisement
	 */
	public ActionForward insert(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String pageId = request.getParameter("pageId");
		String adId = request.getParameter("adId");
		String layout = request.getParameter("layout");

		PageAd pageAd = new PageAd();
		pageAd.setPageId(Integer.valueOf(pageId));
		pageAd.setAdId(Integer.valueOf(adId));
		pageAd.setLayout(layout.equals("H") ? 0 : 1);

		pageAdService.insertAdToPage(pageAd);

		return list(mapping, form, request, response);
	}

	/**
	 * Delete ad
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String[] ids = request.getParameterValues("id");
		int[] pageAdIds = new int[ids.length];
		for (int i = 0; i < ids.length; i++) {
			pageAdIds[i] = Integer.valueOf(ids[i]);
		}
		pageAdService.deletePageAd(pageAdIds);
		return list(mapping, form, request, response);
	}

	/**
	 * Edit ad
	 */
	@SuppressWarnings("unchecked")
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String pageAdId = request.getParameter("pageAdId");
		PageAd pageAd = pageAdService.editPageAd(Integer.valueOf(pageAdId));
		request.setAttribute("pageAd", pageAd);

		List[] lists = pageAdService.load();

		if (lists != null && lists.length == 2) {
			request.setAttribute("contents", lists[0]);
			request.setAttribute("ads", lists[1]);
		}

		return mapping.findForward("fwd_edit_pageAd");
	}

	/**
	 * Update ad
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String pageAdId = request.getParameter("pageAdId");
		String pageId = request.getParameter("pageId");
		String adId = request.getParameter("adId");
		String layout = request.getParameter("layout");

		PageAd pageAd = new PageAd();
		pageAd.setId(Integer.valueOf(pageAdId));
		pageAd.setPageId(Integer.valueOf(pageId));
		pageAd.setAdId(Integer.valueOf(adId));
		pageAd.setLayout(layout.equals("H") ? 0 : 1);

		pageAdService.updatePageAd(pageAd);

		return list(mapping, form, request, response);
	}

	public ActionForward load(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List[] lists = pageAdService.load();

		if (lists != null && lists.length == 2) {
			request.setAttribute("contents", lists[0]);
			request.setAttribute("ads", lists[1]);
		}
		return mapping.findForward("fwd_add_pageAd");
	}
}
